//
//  main.cpp
//  二叉树上机
//
//  Created by Yan Zihao on 2024/12/7.
//

#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <stdbool.h>
typedef char BTDataType;
typedef struct BinaryTreeNode
{
    BTDataType data;
    struct BinaryTreeNode* left;
    struct BinaryTreeNode* right;
}BTNode;

BTNode* BuyNode(char x)
{
    BTNode* node = (BTNode*)malloc(sizeof(BTNode));
    if (node == NULL)
    {
        perror("malloc fail");
        return NULL;
    }

    node->data = x;
    node->left = NULL;
    node->right = NULL;

    return node;
}
BTNode* CreatBinaryTree()
{
    BTNode* node1 = BuyNode('A');
    BTNode* node2 = BuyNode('B');
    BTNode* node3 = BuyNode('C');
    BTNode* node4 = BuyNode('D');
    BTNode* node5 = BuyNode('E');
    BTNode* node6 = BuyNode('F');
    BTNode* node7 = BuyNode('G');


    node1->right = node2;
    node2->left = node3;
    node2->right = node4;
    node4->left = node5;
    node4->right = node6;
    node5->right = node7;

    return node1;
}

void PrevOrder(BTNode* root)
{
    if (root == NULL)
    {
        return;
    }

    printf("%c", root->data);
    PrevOrder(root->left);
    PrevOrder(root->right);
}

void InOrder(BTNode* root)
{
    if (root == NULL)
    {
        return;
    }

    InOrder(root->left);
    printf("%c", root->data);
    InOrder(root->right);
}

void PostOrder(BTNode* root)
{
    if (root == NULL)
    {
        return;
    }

    PostOrder(root->left);
    PostOrder(root->right);
    printf("%c", root->data);
}



int main()
{
    BTNode* root = CreatBinaryTree();
    printf("前序遍历:");
    PrevOrder(root);
    printf("\n");
    printf("中序遍历:");
    InOrder(root);
    printf("\n");
    printf("后序遍历:");
    PostOrder(root);
    printf("\n");
    return 0;
}
